import { CreateModalList } from "@/biz-components/hoc-form/with-list";
import { onFieldChange, onFieldInit, onFieldValueChange, onFormMount } from "@formily/core";
import { message } from "antd";
import { useRef, useState } from "react";
import { history } from "umi";
import _ from 'lodash';
import api, { filecabinetCreate, filecabinetUpdate, filecabinetDelete } from './services';

const WithListComp = CreateModalList({});

const FillingCabinet = (props: any) => {
    const formRef = useRef();
    const { match } = props;
    const [refresh, setRefresh] = useState(false);
    /**
     * 创建、编辑
     * @param values
     */
    const handleSave = async (values: any) => {
      return values.viewType === 'EDIT' ? await filecabinetUpdate({
        ...values,
        pageCode: 'ArcvFileCbnt_CUD'
      }) : await filecabinetCreate({
        ...values,
        pageCode: 'ArcvFileCbnt_CUD'
      });
    };

    return (
      <>
        <WithListComp
          match={match}
          history={history}
          reloadFlag={refresh}
          pageCode={"ArcvFileCbnt_L"}
          pageApi={api.page}
          modalCode={"ArcvFileCbnt_CUD"}
          noModal={false}
          noCache
          handleSave={handleSave}
          formConfigs={{
            effects: (form: any) => {
              formRef.current = form;
            },
          }}
          events={{
            buttonsEvents: {
              // 导入
              // 导出
              // 下载模版
            },
            tableButtonsEvents: {
              handleDeleteClick: (key: string, row: any) => {
                filecabinetDelete({
                  id: row.id,
                  pageCode: 'ArcvFileCbnt_CUD'
                }).then((res: any) => {
                  if (res.success) {
                    message.success('删除成功');
                    setRefresh(!refresh);
                  }
                })
              }
            },
          }}
          modalformConfigs={{
            effects: () => {
              onFieldValueChange('respbl', (field, form) => {
                if (!field.modified) return;
                form.setValuesIn('respblName', field.inputValues?.[1]?.name);
              })
              onFieldChange('belongCo', (field: any, form) => {
                form.setFieldState('whseCode', st => {
                  st.componentProps = _.merge(st.componentProps, {
                    disabled: !field.value,
                    api: {
                      params: {
                        queryKeys: {
                          belongCo: field.value,
                          pageCode: 'ArcvWhse_L'
                        }
                      }
                    }
                  })
                })
                if (!field.modified) return;
                form.setValuesIn('whseCode', undefined);
                form.setValuesIn('whseCodeName', undefined);
                form.setValuesIn('belongCoName', field.inputValues?.[1]?.name);
              })
              onFieldValueChange('type', (field, form) => {
                if (!field.modified) return;
                form.setValuesIn('typeName', field.inputValues?.[2]?.[0]);
              })
              onFieldValueChange('whseCode', (field, form) => {
                if (!field.modified) return;
                form.setValuesIn('whseCodeName', field.inputValues?.[1]?.name);
              })
            }
          }}
        />
      </>
    );
  };

  export default FillingCabinet;
